package controller;

import Pojo.Houselist;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.HouselistService;

@Controller
public class HoustlistController
{

  @Autowired
  private HouselistService houselistService;

  @RequestMapping({"/houselist"})
  public String houselist(Model model, @RequestParam(required=false, defaultValue="1") Integer page, @RequestParam(required=false, defaultValue="5") Integer pageSize)
  {
    PageHelper.startPage(page.intValue(), pageSize.intValue());
    List houselist = this.houselistService.selectAll();
    PageInfo p = new PageInfo(houselist);

    model.addAttribute("p", p);
    model.addAttribute("houselist", houselist);
    model.addAttribute("mainPage", "houselist.jsp");
    return "zuke/main";
  }

  @RequestMapping({"/ahouselist"})
  public String ahouselist(Model model, @RequestParam(required=false, defaultValue="1") Integer page, @RequestParam(required=false, defaultValue="5") Integer pageSize)
  {
    PageHelper.startPage(page.intValue(), pageSize.intValue());
    List houselist = this.houselistService.selectAll();
    PageInfo p = new PageInfo(houselist);

    model.addAttribute("p", p);
    model.addAttribute("houselist", houselist);
    model.addAttribute("mainPage", "ahouselist.jsp");
    return "admin/main1";
  }

  @RequestMapping({"/addhouse"})
  public String addhouse(Model model, Houselist houselist)
  {
    String houseid = houselist.getHouseid();
    Houselist houselist1 = this.houselistService.findhouseid(houseid);
    if (houselist1 != null) {
      model.addAttribute("error", "该房屋id已存在");
      model.addAttribute("houselist", houselist);
      model.addAttribute("mainPage", "addhouse.jsp");
      return "admin/main1";
    }
    model.addAttribute("error", "添加成功");
    this.houselistService.inserthouse(houselist);
    model.addAttribute("houselist", houselist);
    model.addAttribute("mainPage", "addhouse.jsp");
    return "admin/main1";
  }

  @RequestMapping({"/toaddhouse"})
  public String toaddhoust(Model model)
  {
    model.addAttribute("mainPage", "addhouse.jsp");

    return "admin/main1"; }

  @RequestMapping({"/deletehouse"})
  public String deletehouse(Integer id) {
    this.houselistService.deletehouse(id.intValue());

    return "redirect:ahouselist.action";
  }

  @RequestMapping({"/toahouselist"})
  public String toahouselist()
  {
    return "ahouselist.action"; }

  @RequestMapping({"/findid"})
  public String findid(Integer id, Model model) {
    Houselist list = this.houselistService.findid(id.intValue());
    model.addAttribute("houselist", list);
    model.addAttribute("mainPage", "changehouse.jsp");
    return "admin/main1"; }

  @RequestMapping({"/findhouseidupdate"})
  public String findhouseidupdate(Houselist houselist, Model model) {
    Houselist list = this.houselistService.findhouseidupdate(houselist);
    if (list != null) {
      model.addAttribute("houselist", houselist);
      model.addAttribute("mainPage", "changehouse.jsp");
      model.addAttribute("error", "该房屋id已存在");
      return "admin/main1";
    }

    this.houselistService.updatehouse(houselist);
    model.addAttribute("houselist", houselist);
    model.addAttribute("mainPage", "changehouse.jsp");
    model.addAttribute("error", "更新成功");
    return "admin/main1";
  }
}